import pygame
from pytmx import TiledObjectGroup, load_pygame

from Actors.Chinese import ChineseSoldier
from Actors.Japanese import JapaneseSoldier
from Actors.Lead_Actor import LeadActor
from Actors.Prizes_Sprite import PrizesSprite
from Scene.Base_Scene import BaseScene, ScenePassStatus
from Task.BaseTask import TaskStatus
from Task.Task1.Task_Collect import CollectTask
from Task.Task1.Task_KillJapanese import KillJapaneseTask


class MainScene(BaseScene):
    """
    游戏画面主要场景
    """

    def __init__(self, lead_actor, background_path, tiled_map_data):
        super(MainScene, self).__init__(background_path)
        self.lead_actor = lead_actor
        self.japanese_stand_group = pygame.sprite.Group()
        self.japanese_run_group = pygame.sprite.Group()
        self.japanese_group = pygame.sprite.Group()
        self.prizes_group = pygame.sprite.Group()
        self.obstacle_group = pygame.sprite.Group()
        self.down_group = pygame.sprite.Group()  # Task3中下坠的识别物
        self.tiled_map_data = load_pygame(tiled_map_data)
        self.init_character()
        self.init_task()

    def init_task(self):
        """
        初始化任务
        :return:
        """
        pass

    def init_music(self):
        pass
        # pygame.mixer.music.load("../Resources/sound/十送红军.mp3")
        # pygame.mixer.music.play()

    def init_character(self):
        """
        初始化场景中的人物
        """
        for group in self.tiled_map_data.objectgroups:
            if isinstance(group, TiledObjectGroup):
                if group.name == "日军":
                    for obj in group:
                        if obj.name == "1":  # 行走的
                            japanese = JapaneseSoldier(obj.x, obj.y)
                            self.japanese_run_group.add(japanese)
                            self.japanese_group.add(japanese)
                        if obj.name == "2":  # 站立的
                            japanese = JapaneseSoldier(obj.x, obj.y)
                            self.japanese_stand_group.add(japanese)
                            self.japanese_group.add(japanese)

                elif group.name == "物品":
                    for obj in group:
                        prizes = PrizesSprite(obj.x, obj.y)
                        prizes.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.prizes_group.add(prizes)

                elif group.name == "空气墙":
                    for obj in group:
                        obstacle = pygame.sprite.Sprite()
                        obstacle.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.obstacle_group.add(obstacle)

                elif group.name == "下坠物":
                    for obj in group:
                        down = pygame.sprite.Sprite()
                        down.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.down_group.add(down)

                elif group.name == "主角":
                    for obj in group:
                        if obj.name == "主角":
                            self.lead_actor = LeadActor(obj.x, obj.y)

                elif group.name == "队友":
                    for obj in group:
                        if obj.name == "队长":
                            self.captain = ChineseSoldier(obj.x, obj.y)
                        if obj.name == "副队长":
                            self.vice_captain = ChineseSoldier(obj.x, obj.y)

    def draw_actor(self):
        """
        画人物
        :return:
        """
        for obj in self.japanese_group:
            obj.draw(self.current_surface, self.view_x + 10, self.view_y + 20)
            x, y, w, h = obj.rect
            # pygame.draw.rect(self.current_surface, (255, 0, 0),
            #                  (x - self.view_x, y - self.view_y, w, h), 2)

        for obj in self.prizes_group:
            obj.draw(self.current_surface, self.view_x, self.view_y)
            # x, y, w, h = obj.rect
            # pygame.draw.rect(self.current_surface, (255, 250, 0),
            #                  (x - self.view_x, y - self.view_y, w, h), 2)

        # for obj in self.obstacle_group:
        #     x, y, w, h = obj.rect
        #     pygame.draw.rect(self.current_surface, (255, 255, 255),
        #                      (x - self.view_x, y - self.view_y, w, h), 2)

        # for obj in self.down_group:
        #     x, y, w, h = obj.rect
        #     pygame.draw.rect(self.current_surface, (255, 255, 255),
        #                      (x - self.view_x, y - self.view_y, w, h), 2)

        self.lead_actor.draw(self.current_surface, self.view_x, self.view_y)
        self.tasks[self.task_id].draw(self.current_surface, self.view_x, self.view_y)

    def run(self, key_down_flag, key_list):
        """
        人物的动作
        :param key_down_flag:
        :param key_list:
        :return:
        """
        self.lead_actor.run(key_down_flag, key_list, self.obstacle_group, self.japanese_group)
        for japanese in self.japanese_stand_group:
            japanese.stand(key_down_flag, key_list, self.obstacle_group)

        for japanese in self.japanese_run_group:
            japanese.run(key_down_flag, key_list, self.obstacle_group)

        if key_down_flag and key_list[pygame.K_1]:
            self.next_status = ScenePassStatus.final_win

        if key_down_flag and key_list[pygame.K_2]:
            self.next_status = ScenePassStatus.final_fail

        self.tasks[self.task_id].do_task(key_down_flag, key_list)

        if self.tasks[self.task_id].get_state() == TaskStatus.win_over:
            self.task_id += 1
            if self.task_id >= len(self.tasks):
                self.next_status = ScenePassStatus.final_win
                pygame.mixer.music.stop()
        elif self.tasks[self.task_id].get_state() == TaskStatus.fail_over:
            self.next_status = ScenePassStatus.final_fail

    def get_next_status(self):
        """
        获取下一状态（需要重写）
        :return:
        """
        # if self.next_status == ScenePassStatus.final_win:
        #     return self.next_status, "WinScene"
        # elif self.next_status == ScenePassStatus.final_fail:
        #     return self.next_status, "FailScene"
        # else:
        #     return self.next_status, None
